///<reference path='./index.d.ts'/>
import store from "store2";
/**存储数据到localStorage */
export default class CddStore {
    constructor(params) {
        if (params && params.length > 0) {
            for (let item of params) {
                // 设置时效
                // 时效，默认半天
                let expires = 0;
                expires += item.days !== undefined ? item.days * CddStore.day : 0;
                expires += item.minutes !== undefined ? item.minutes * CddStore.minute : 0;
                expires += item.hours !== undefined ? item.hours * CddStore.hour : 0;
                if (item.forever) {
                    expires = 2 * 365 * CddStore.day;
                }
                else {
                    expires === 0 && (expires = CddStore.day * .5);
                }
                this[item.key] = new Entire(Object.assign({ expires }, item));
            }
        }
    }
}
CddStore.minute = 1000 * 60;
CddStore.hour = 60 * CddStore.minute;
CddStore.day = 24 * CddStore.hour;
/**存储数据格式类 */
class Entire {
    constructor(params) {
        this.stamp = Date.now();
        this.aotuRefresh = true;
        this.autorenewal = false;
        // 如果存在则进行赋值
        const data = store.get(params.key);
        if (!params.value && data) {
            if (Date.now() - data.stamp < data.expires) {
                params.value = data._value;
            }
        }
        this.key = params.key;
        if (!params.value && params.refresh) { //默认赋值
            params.value = params.refresh();
        }
        params.refresh && (this.refresh = params.refresh);
        this._value = params.value;
        this.expires = params.expires;
        params.autorenewal !== undefined && (this.autorenewal = params.autorenewal);
        params.aotuRefresh !== undefined && (this.aotuRefresh = params.aotuRefresh);
        store.set(params.key, this);
    }
    /**值 */
    get value() {
        if (this.isOver) {
            store.remove(this.key);
            if (this.autorenewal && !this.refresh) {
                this.setNewExpires();
            }
            else if (this.aotuRefresh && this.refresh) {
                this.setValue(this.refresh());
                return this._value;
            }
            else {
                console.log(`${this.key}已过期`);
                return undefined;
            }
        }
        return this._value;
    }
    /**获取值 */
    getValue() {
        if (this.isOver) {
            store.remove(this.key);
            if (this.autorenewal && !this.refresh) {
                this.setNewExpires();
            }
            else if (this.aotuRefresh && this.refresh) {
                this.setValue(this.refresh());
                return this._value;
            }
            else {
                console.log(`${this.key}已过期`);
                return undefined;
            }
        }
        return this._value;
    }
    /**设置值*/
    setValue(value) {
        this._value = value;
        this.setNewExpires();
    }
    /**重写设置延续时间 */
    setNewExpires(duration) {
        this.stamp = Date.now();
        this.expires = duration || this.expires;
        store.set(this.key, this);
    }
    /**延期*/
    renewal() {
        if (this.refresh) {
            this._value = this.refresh();
        }
        this.setNewExpires();
    }
    /**是否超时 */
    get isOver() {
        return this._value !== undefined && Date.now() - this.stamp >= this.expires;
    }
}
